package com.bilifuture.statemachine.machine;


import com.bilifuture.statemachine.exception.StateMachineException;
import com.bilifuture.statemachine.interceptor.StateMachineInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 状态机加载后维护
 * @author bilifuture
 * @description:
 * @date 2022-02-23
 */
public class StateMachineHolder {
    
    /**
     * 状态机映射集合
     */
    private ConcurrentHashMap<String, StateMachineConfig> stateMachineMap = new ConcurrentHashMap<>();
    
    /**
     * 全局拦截器列表
     */
    private List<StateMachineInterceptor> globalInterceptorList = new ArrayList<>();
    
    /**
     * 添加拦截
     *
     * @param globleInterceptorList 全局拦截器列表
     */
    public void addInterceptorList(List<StateMachineInterceptor> globleInterceptorList){
        this.globalInterceptorList.addAll(globleInterceptorList);
    }
    
    /**
     * 添加状态机
     *
     * @param stateMachineMap 状态机映射
     */
    public void addStateMachine(Map<String, StateMachineConfig> stateMachineMap){
        this.stateMachineMap.putAll(stateMachineMap);
    }
    
    public StateMachineConfig getStateMachineConfig(String stateMachineName){
        StateMachineConfig stateMachineConfig = stateMachineMap.get(stateMachineName);
        if(Objects.isNull(stateMachineConfig)){
            throw new StateMachineException("未找到对应的状态机配置信息");
        }
        return stateMachineConfig;
        
    }
    
    public List<StateMachineInterceptor> getGlobalInterceptorList() {
        return globalInterceptorList;
    }
}
